using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class CategoryDAL
    {
        public static int Save(CategoryInfo info)
        {
            string sql = string.Format("insert into [category]([AdminID],[ParentID],[Name],[Sort],[Img]) values(@AdminID,@ParentID,@Name,@Sort,@Img);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@ParentID", info.ParentID),
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Sort", info.Sort),
                new SqlParameter("@Img", info.Img)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(CategoryInfo info)
        {
            string sql = string.Format("update [category] set [AdminID]=@AdminID,[Name]=@Name,[Sort]=@Sort,[Img]=@Img where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Sort", info.Sort),
                new SqlParameter("@Img", info.Img),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static int Edit(CategoryInfo info)
        {
            string sql = string.Format("update [category] set [Name]=@Name,[MenuIcon]=@MenuIcon,[MenuSort]=@MenuSort,[MenuIsTop]=@MenuIsTop where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@MenuIcon", info.MenuIcon),
                new SqlParameter("@MenuSort", info.MenuSort),
                new SqlParameter("@MenuIsTop", info.MenuIsTop),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static int Modify(CategoryInfo info)
        {
            string sql = string.Format("update [category] set [Name]=@Name,[ModuleSort]=@ModuleSort,[ModuleIsTop]=@ModuleIsTop,[Count]=@Count where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@ModuleSort", info.ModuleSort),
                new SqlParameter("@ModuleIsTop", info.ModuleIsTop),
                new SqlParameter("@Count", info.Count),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static CategoryInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[category]");
            return SimpleData(dr);
        }
        public static List<CategoryInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[category]");
            return ListSimpleData(dt);
        }
        public static List<CategoryInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [category].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [category].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[category]", out count);
            return ListSimpleData(dt);
        }
        public static List<CategoryInfo> FindAll(int adminid)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [category] where AdminID={0} order by Sort",adminid));
            return ListSimpleData(dt);
        }
        private static CategoryInfo SimpleData(DataRow dr)
        {
            CategoryInfo info = new CategoryInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.ParentID = DataConvert.ToInt(dr["ParentID"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Sort = DataConvert.ToInt(dr["Sort"]);
                info.Img = Commons.domain + DataConvert.ToString(dr["Img"]);
                info.MenuIcon = DataConvert.ToString(dr["MenuIcon"]);
                info.MenuSort = DataConvert.ToInt(dr["MenuSort"]);
                info.MenuIsTop = DataConvert.ToInt(dr["MenuIsTop"]);
                info.ModuleSort = DataConvert.ToInt(dr["ModuleSort"]);
                info.ModuleIsTop = DataConvert.ToInt(dr["ModuleIsTop"]);
                info.Count = DataConvert.ToInt(dr["Count"]);
            }
            return info;
        }
        private static CategoryInfo AllData(DataRow dr)
        {
            CategoryInfo info = new CategoryInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.ParentID = DataConvert.ToInt(dr["ParentID"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Sort = DataConvert.ToInt(dr["Sort"]);
                info.Img = Commons.domain + DataConvert.ToString(dr["Img"]);
                info.MenuIcon = DataConvert.ToString(dr["MenuIcon"]);
                info.MenuSort = DataConvert.ToInt(dr["MenuSort"]);
                info.MenuIsTop = DataConvert.ToInt(dr["MenuIsTop"]);
                info.ModuleSort = DataConvert.ToInt(dr["ModuleSort"]);
                info.ModuleIsTop = DataConvert.ToInt(dr["ModuleIsTop"]);
                info.Count = DataConvert.ToInt(dr["Count"]);

            }
            return info;
        }
        private static List<CategoryInfo> ListSimpleData(DataTable dt)
        {
            List<CategoryInfo> list = new List<CategoryInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<CategoryInfo> ListAllData(DataTable dt)
        {
            List<CategoryInfo> list = new List<CategoryInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

